package br.ufsm.inf.zebraloqacliente.activities;

import br.ufsm.inf.zebraloqacliente.R;

import android.os.Bundle;
import com.google.zxing.integration.android.IntentIntegrator;
import com.google.zxing.integration.android.IntentResult;

import android.app.ActionBar;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class LerQRCodeActivity extends Activity implements OnClickListener {

	//  Instanciando o resultado
	private Button scanBtn;
	private TextView formato, resultado;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		
		super.onCreate(savedInstanceState);
		setContentView(R.layout.ler_code);

		scanBtn = (Button)findViewById(R.id.scanBtn);
		formato = (TextView)findViewById(R.id.scan_formato);
		resultado = (TextView)findViewById(R.id.scan_resultado);

		scanBtn.setOnClickListener (this);
	}
	

	public void onClick(View v){
		//verifica botao
		if(v.getId()==R.id.scanBtn){
			//Instancia a classe do Leitor
			IntentIntegrator scanIntegrator = new IntentIntegrator(this);
			//inicia Scaneamento
			scanIntegrator.initiateScan();
		}
	}

	public void onActivityResult(int requestCode, int resultCode, Intent intent) {
		//instancia e recebe o resultado
		IntentResult scanningResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, intent);
		//verifica o resultado válido
		if (scanningResult != null) {
			//dá um get no resultado do scaneamento
			String scanContent = scanningResult.getContents();
			//dá um get no formato
			String scanFormat = scanningResult.getFormatName();
			//Saída para interface
			formato.setText("Formato: "+scanFormat);
			resultado.setText("Resultado: "+scanContent);
			
			// inicia a activity que busca o produto:
			Intent i = new Intent(getApplicationContext(), BrowserActivity.class);
        	i.putExtra("url", "http://zebraloqa.pythonanywhere.com/loja/produto/" + scanContent);
            startActivity(i);
		}
		else {
			//Formato inválido ou scaneamento cancalado
			Toast toast = Toast.makeText(getApplicationContext(), 
					"Erro ao scannear!", Toast.LENGTH_SHORT);
			toast.show();
		}
	}
	
	@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        
        ActionBar actionBar = getActionBar();
        //actionBar.setSubtitle("Zebra Loqa");
        actionBar.setTitle(""); 
        
        return true;
    }
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
    	Intent i;
        switch (item.getItemId()) {
	      	case R.id.busca:
	      		i = new Intent(getApplicationContext(), BrowserActivity.class);
	        	i.putExtra("url", "http://zebraloqa.pythonanywhere.com/");
	            startActivity(i);
	      		break;
	    	  
	      	case R.id.qr_code:	      		
	      		i = new Intent(getApplicationContext(), LerQRCodeActivity.class);
                startActivity(i);
	      		break;
	        
	      	case R.id.perfil:
	      		i = new Intent(getApplicationContext(), BrowserActivity.class);
	        	i.putExtra("url", "http://zebraloqa.pythonanywhere.com/accounts/profile/");
	            startActivity(i);
	      		break;
	          
	      	case R.id.carrinho:
	      		i = new Intent(getApplicationContext(), BrowserActivity.class);
	        	i.putExtra("url", "http://zebraloqa.pythonanywhere.com/loja/carrinho/last/");
	            startActivity(i);
	      		break;
	
	      	default:
	      		break;
        }

        return true;
    }

}
